export default function getAge(birthday, currentDate) {
  try {
    let reg = new RegExp(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
    let birthdayArr = birthday.match(reg);
    let currentDateArr = currentDate.match(reg);
    let beginArr = [];
    let endArr = [];

    let days = 0;
    let month = 0;
    let year = 0;

    days = currentDateArr[4] - birthdayArr[4];
    if (days < 0) {
      month = -1;
      days = 30 + days;
    }

    month = month + (currentDateArr[3] - birthdayArr[3]);
    if (month < 0) {
      year = -1;
      month = 12 + month;
    }

    year = year + (currentDateArr[1] - birthdayArr[1]);

    let yearString = year > 0 ? year + "岁" : "";
    let mnthString = month > 0 ? month + "个月" : "";
    let dayString = days > 0 ? days + "天" : "";

    /*
     * 1 如果岁 大于等于1 那么年龄取 几岁 2 如果 岁等于0 但是月大于1 那么如果天等于0
     天小于3天 取小时
     * 例如出生2天 就取 48小时
     */
    let result = "";
    if (year >= 100) {
      result = "未设置"
    } else if (year >= 1) {
      result = yearString + mnthString;
    } else {
      if (month >= 1) {
        result = days > 0 ? mnthString + dayString : mnthString;
      } else {
        let begDate = new Date(beginArr[1], beginArr[3] - 1,
          beginArr[4], beginArr[6], beginArr[8], beginArr[10]);
        let endDate = new Date(endArr[1], endArr[3] - 1, endArr[4],
          endArr[6], endArr[8], endArr[10]);

        let between = (endDate.getTime() - begDate.getTime()) / 1000;
        days = Math.floor(between / (24 * 3600));
        let hours = Math.floor(between / 3600 - (days * 24));
        dayString = days > 0 ? days + "天" : "";
        result = days >= 3 ? dayString : days * 24 + hours + "小时";
      }
    }

    return result;
  } catch (error) {
    return "";
  }
}
